package com.todofun.business.tuanke.bo;

import java.util.List;

import com.todofun.business.tuanke.vo.ApplyRecord;
import com.todofun.business.tuanke.vo.Campaign;
import com.todofun.common.app.BaseService;
import com.todofun.common.app.MailTools;
import com.todofun.common.util.CommonUtil;

public class ApplyRecordService extends BaseService {

	public void addNewApplyRecord(ApplyRecord applyRecord) {
		String uuid = CommonUtil.getUUID();
		String curtime = CommonUtil.getCurrentTime();
		Long campaignId = applyRecord.getCampaignId();
		Campaign campaign = (Campaign) super.getVoByID(Campaign.class, campaignId); 
		applyRecord.setApplyCost(campaign.getLesson().getCost());
		applyRecord.setApplyTime(CommonUtil.getCurrentTime());
		applyRecord.setTuanCode(uuid);
		super.saveVo(applyRecord);
		
		MailTools tool = new MailTools();
		//发送管理员
		tool.sendMail("有新的用户来报名",
				"用户姓名： "+ applyRecord.getUserName()+"\r\n"+
				"Email： "+ applyRecord.getEmail()+"\r\n"+
				"手机： "+ applyRecord.getTelephone()+"\r\n"+
				"团购码： "+ uuid+"\r\n"+
				"报名时间："+ curtime,
				"wugang021874@126.com");
		
		//发送学生
		tool.sendMail("报名成功！请确认您的报名信息",
				applyRecord.getUserName()+" 您好！请确认您的报名信息：\r\n\r\n"+
				"姓名： "+ applyRecord.getUserName()+"\r\n"+
				"Email： "+ applyRecord.getEmail()+"\r\n"+
				"手机： "+ applyRecord.getTelephone()+"\r\n"+
				"团购码： "+ uuid+"\r\n"+
				"所报课程： "+ campaign.getLesson().getName()+"\r\n"+
				"开班时间： "+ campaign.getEndDate()+"\r\n"+
				"课程价格： "+ campaign.getLesson().getCost()+"\r\n"+
				"课程优惠： "+ campaign.getDiscountComments()+"\r\n"+				
				"报名时间："+ curtime + "\r\n\r\n请您凭团购码享受报班"+ campaign.getDiscountComments() +"优惠，如有疑问请在线咨询QQ317578473！",
				applyRecord.getEmail().trim());
		
		//发送商家
		/*
		tool.sendMail("课程报名",
				"用户姓名： "+ applyRecord.getUserName()+"\r\n"+
				"Email： "+ applyRecord.getEmail()+"\r\n"+
				"手机： "+ applyRecord.getTelephone()+"\r\n"+
				"团购码： "+ uuid+"\r\n"+
				"所报课程： "+ campaign.getLesson().getName()+"\r\n"+
				"开班时间： "+ campaign.getEndDate()+"\r\n"+
				"课程价格： "+ campaign.getLesson().getCost()+"\r\n"+
				"课程优惠： "+ campaign.getDiscountComments()+"\r\n"+		
				"报名时间："+ curtime + "\r\n\r\n本信息来至http://www.5iitt.com" ,
				"wg@cic.tsinghua.edu.cn");
				*/
	}


	public List<ApplyRecord> listAllRecord() {
		String hql = "from ApplyRecord order by id desc";
		List<ApplyRecord> rs = super.queryByHQL(hql);
		return rs;
	}

	public void markRecordPayed(Long recordId,boolean isPayed) {
		ApplyRecord applyRecord = (ApplyRecord) super.getVoByID(ApplyRecord.class, recordId);
		applyRecord.setIsPayed(isPayed);
		super.updateVo(applyRecord);
	}

	public void markGotMoneyFromRecord(Long recordId,boolean isGotMoney) {
		ApplyRecord applyRecord = (ApplyRecord) super.getVoByID(ApplyRecord.class, recordId);
		applyRecord.setIsGotMoney(isGotMoney);
		super.updateVo(applyRecord);
		
	}

}
